/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import util.HibernateUtil;
import java.util.List;
import pojo.Thanhvien;
import org.hibernate.Query;
import org.hibernate.Session;
/**
 *
 * @author Jackhit
 */
public class ThanhVienDAO {
    public List<Thanhvien> layDanhSachThanhVien() {
        List<Thanhvien> dsThanhVien = null;
         Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            String hql = "from Thanhvien";
            Query query = session.createQuery(hql);
            dsThanhVien = query.list();
         }catch(Exception ex) {
            System.out.println(ex.getMessage());
        }finally {
            session.close();
        }
        return dsThanhVien;
    }

    public boolean themThanhVien(Thanhvien thanhVien) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(thanhVien);
            session.getTransaction().commit();
            return true;
        }catch(Exception e) {
            session.getTransaction().rollback();
            return false;
        }
        finally {
            session.close();
        }
    }

    public boolean capNhatThanhVien(Thanhvien thanhVien) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(thanhVien);
            session.getTransaction().commit();
            return true;
        }catch(Exception e) {
            session.getTransaction().rollback();
            return false;
        }
        finally {
            session.close();
        }
    }

    public boolean xoaThanhVien(Thanhvien thanhVien) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(thanhVien);
            session.getTransaction().commit();
            return true;
        }catch(Exception e) {
            session.getTransaction().rollback();
            return false;
        }
        finally {
            session.close();
        }
    }

    public static Thanhvien dangNhap(String username, String password) {
        Thanhvien thanhVien = null;
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            String hql = "from ThanhVienPOJO where tenDangNhap = :tenDangNhap and matKhau = :matKhau";
            Query query = session.createQuery(hql);
            query.setString("tenDangNhap", username);
            query.setString("matKhau", password);
            thanhVien = (Thanhvien) query.uniqueResult();
        }catch(Exception ex) {
            System.out.println(ex.getMessage());
        }
        finally {
            session.close();
        }
        return thanhVien;
    }
}
